#include "system.h"

#include "etc/os-release.h"
#include "proc/uptime.h"

bool proc_systeminfo_of_buffer(char *buf, System *s) {
    if (isNull(buf)) return false;

    GSList *lines = proc_cmdlineSplit(buf);
    // g_slist_foreach(lines, (GFunc)printf_iterator, "-->");
    for (GSList *iter = lines; iter; iter=iter->next) {
        proc_cmdline_of_buffer(iter->data, &s->cmdline);
    }
    GS_FREE_ALL_CLEAN(lines)

    return true;
}

bool initSystem(System *s) {

    // DEBUG_LOG("// Cmdline");
    // Cmdline
    char buffer[10000] = {0};
    if(FileReadToBuffer(LINUX_OS_PROC_CMDLINE, buffer, sizeof(buffer))) {
        proc_systeminfo_of_buffer(buffer, s);
    }

    // DEBUG_LOG("// Kernel Version");
    // Kernel Version
    S_CLEARN(buffer);
    if (getCmdStdoutLine(SYSTEM_KERNEL_VERSION_CMD, buffer)) {
        #ifdef __linux__
        strcat(s->Kernel_Version, "Linux version ");
        #endif  //
        strcat(s->Kernel_Version, buffer);
    }

    // DEBUG_LOG("// Architecture");
    // Architecture
    S_CLEARN(buffer);
    if (foundCommand(SYSTEM_ARCH_CMD)) {
        if (getCmdStdoutLine(SYSTEM_ARCH_CMD, buffer)) {
            strcpy(s->Architecture, buffer);
        }
    } else if(foundCommand(SYSTEM_UNAME_CMD)) {
        if (getCmdStdoutLine(SYSTEM_UNAME_MACHINE_CMD, buffer)) {
            strcpy(s->Architecture, buffer);
        }
    }

    // DEBUG_LOG("// Runtime");
    // Runtime
    S_CLEARN(buffer);
    Uptime up = {0};
    if (initUptime(&up)) {
        if (proc_uptime_to_buffer(&up, buffer)) {
            strcpy(s->Runtime, buffer);
        }
    }

    // DEBUG_LOG("// Systime");
    // Systime
    S_CLEARN(buffer);
    if (foundCommand(SYSTEM_DATE_CMD) && getCmdStdoutLine(SYSTEM_DATE_TIME_CMD, buffer)) {
            strcpy(s->Systime, buffer);
    } else {
        timestampf(buffer, TIME_STAMP_FORMAT);
        strcpy(s->Systime, buffer);
    }

    // DEBUG_LOG("// Load");
    // Load
    S_CLEARN(buffer);
    if (foundCommand(SYSTEM_UPTIME_CMD) && getCmdStdoutLine(SYSTEM_UPTIME_CMD, buffer)) {
        char *load_average = NULL;

#ifdef __linux__
        if (LineHasValue(buffer, CPU_LOAD_KEY)) {
            GSList *splitT = splits(buffer, CPU_LOAD_KEY);
#elif __APPLE__
        if (LineHasValue(buffer, "averages:")) {
            GSList *splitT = splits(buffer, "averages:");
#endif
            if (splitT != NULL && g_slist_length(splitT) == 2) {
                load_average = LineTrim(g_slist_nth(splitT, 1)->data);
                strcpy(s->Load, load_average);

                free(load_average);
            }
            GS_FREE_ALL_CLEAN(splitT)
        }
    }

    // DEBUG_LOG("// Firmware Version");
    // Firmware Version
    S_CLEARN(buffer);
    OS_Release os = {0};
    if(initOsRelease(&os)) {
        strcpy(s->Firmware_Version, os.version);
    }

    return true;
}

// usage: 
// ┌──────────────────────────────────────────────────────────────────────────────┐
// │ System info = {0};                                                           │
// │ initSystem(&info);                                                           │
// └──────────────────────────────────────────────────────────────────────────────┘


// ------------------------------------ CmdLine Test
